/*
 * @Author: wangjun haodreams@163.com
 * @Date: 2024-07-20 16:57:29
 * @LastEditors: wangjun haodreams@163.com
 * @LastEditTime: 2024-07-22 22:08:23
 * @FilePath: \goui\ui\Libel.go
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package gw

import (
	"image/color"

	"gioui.org/layout"
	"gioui.org/widget/material"
)

type statusBar struct {
	win   *Window
	texts []string
	layout.Flex
	showStatusBar bool
}

func (m *statusBar) Init(win *Window, size int) {
	m.win = win
	m.texts = make([]string, size)
}

func (m *statusBar) SetStatusBarText(idx int, text string) {
	if idx < 0 || idx >= len(m.texts) {
		return
	}
	m.texts[idx] = text
}

func (m *statusBar) ShowStatusBar(b bool) {
	m.showStatusBar = b
}

func (m *statusBar) Layout(gtx layout.Context) layout.Dimensions {
	n := len(m.texts)
	fileds := make([]layout.FlexChild, n)
	n--
	for i := 0; i <= n; i++ {
		if i == 0 {
			body := material.Body1(m.win.theme, m.texts[i])
			body.Truncator = "#"
			body.MaxLines = 1
			fileds[i] = layout.Flexed(2, body.Layout)
		} else {
			body := material.Body1(m.win.theme, m.texts[i])
			body.Truncator = "#"
			body.MaxLines = 1
			if i == n {
				body.Color = color.NRGBA{R: 255, G: 0, B: 0, A: 255}
				fileds[i] = layout.Flexed(1, body.Layout)
			}
		}
	}
	return m.Flex.Layout(gtx, fileds...)
}
